package qeb.handheldmedical.patient;

import java.text.BreakIterator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.helper.DateTimePickDialogUtil;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.BloodSugar;
import qeb.handheldmedical.model.InPatient;
import qeb.handheldmedical.model.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2017年2月27日 下午4:07:58
 * @declaration
 */
public class AddBloodSugar extends Activity {
    private InPatient pi;
    private User usr;
    String initStartDateTime;
    private Button btn_title_back;
    private Button btn_title_right;
    private TextView tv_title;
    private EditText edtxtClsj, edtbloodSugar;
    private Button btnAddXtOk;
    private BloodSugar bloodSugar;
    public String strAutoid = "";

    /*
     * (non-Javadoc)
     *
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_bloods_sugar);
        pi = (InPatient) getIntent().getSerializableExtra("patientinfo");
        usr = (User) getIntent().getSerializableExtra("userinfo");
        bloodSugar = (BloodSugar) getIntent()
                .getSerializableExtra("bloodSugar");
        initView();
    }

    private void initView() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        initStartDateTime = sDateFormat.format(new java.util.Date());

        tv_title = (TextView) findViewById(R.id.tv_top_title);
        tv_title.setText(pi.XM + "的血糖记录");
        btn_title_back = (Button) findViewById(R.id.btn_title_back);
        btn_title_back.setOnClickListener(MyOnClickListener);
        btn_title_right = (Button) findViewById(R.id.btn_title_right);
        btn_title_right.setVisibility(View.VISIBLE);
        btnAddXtOk = (Button) findViewById(R.id.btnAddXtOk);
        btnAddXtOk.setOnClickListener(MyOnClickListener);
        edtxtClsj = (EditText) findViewById(R.id.edtxtClsj);
        edtbloodSugar = (EditText) findViewById(R.id.edtbloodSugar);
        edtxtClsj.setText(initStartDateTime);

        if (bloodSugar != null) {
            edtxtClsj.setText(bloodSugar.CLSJ);
            edtbloodSugar.setText(bloodSugar.CLJG);
            strAutoid = bloodSugar.AUTOID;
        }

        edtxtClsj.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
                        AddBloodSugar.this, initStartDateTime);
                dateTimePicKDialog.dateTimePicKDialog(edtxtClsj);

            }
        });
    }

    private OnClickListener MyOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO 自动生成的方法存根
            // Bundle bundle=new Bundle();

            Intent intent = new Intent();

            switch (v.getId()) {
                case R.id.btn_title_back:// 返回按钮
                    intent.putExtra("UPDATE", "0");
                    AddBloodSugar.this.setResult(RESULT_OK, intent);
                    AddBloodSugar.this.finish();
                    return;
                case R.id.btnAddXtOk:// 保存更新体温
                    String strXt = edtbloodSugar.getText().toString();
                    String strClsj = edtxtClsj.getText().toString();
                    String jsonString = "";
                    if (!strXt.equals("")) {

                        jsonString = String
                                .format("{\"AUTOID\":\"%1$s\",\"CLSJ\":\"%2$s\",\"CLJG\":\"%3$s\",\"CLHS\":\"%4$s\",\"CDHSN\":\"%5$s\",\"JLSH\":\"%6$s\",\"JLHSN\":\"%7$s\",\"ZYXH\":\"%8$s\"}",
                                        strAutoid, strClsj, strXt, usr.DID,
                                        usr.DNAME, usr.DID, usr.DNAME, pi.ZYXH);

                        Map<String, String> map = new HashMap<String, String>();
                        map.put("xtjlson", jsonString);

                        if (strAutoid.equals("")) {
                            map.put("type", "1");
                        } else {
                            map.put("type", "2");
                        }

                        JSONArray rejson = WSHelper.getWebserviceFun(
                                "InsertOrUpdateXtjl", map);
                        try {
                            JSONObject reObject = rejson.getJSONObject(0);
                            if (reObject.getString("pda_error_state").equals("0")) {
                                intent.putExtra("UPDATE", "1");
                            } else {
                                intent.putExtra("UPDATE", "0");
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        AddBloodSugar.this.setResult(RESULT_OK, intent);
                        AddBloodSugar.this.finish();
                    }
                    return;
            }
        }
    };
}
